using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;
using Acme.BookStore.Domain.Entities;
using Acme.BookStore.EntityFrameworkCore;
using Acme.BookStore.Repositories;
using Microsoft.EntityFrameworkCore;
using Volo.Abp.Domain.Repositories.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore;

namespace Acme.BookStore.Repositories
{
    public class EfCoreGongyingRepository : EfCoreRepository<BookStoreDbContext, Gongying, string>, IGongyingRepository
    {
        public EfCoreGongyingRepository(IDbContextProvider<BookStoreDbContext> dbContextProvider)
            : base(dbContextProvider)
        {
        }

        public async Task<List<Gongying>> GetListAsync(
            int skipCount,
            int maxResultCount,
            string sorting,
            string filter = null)
        {
            var dbSet = await GetDbSetAsync();
            var query = dbSet.AsQueryable();

            // 基本过滤
            if (!string.IsNullOrWhiteSpace(filter))
            {
                query = query.Where(g =>
                    g.FullName.Contains(filter) ||
                    g.MainContact.Contains(filter) ||
                    g.Address.Contains(filter) ||
                    g.Phone.Contains(filter) ||
                    g.Mobile.Contains(filter));
            }

            // 排序
            if (!string.IsNullOrWhiteSpace(sorting))
            {
                query = query.OrderBy(sorting);
            }
            else
            {
                query = query.OrderBy(g => g.Id);
            }

            // 分页
            return await query
                .Skip(skipCount)
                .Take(maxResultCount)
                .ToListAsync();
        }

        public async Task<int> GetCountAsync(string filter = null)
        {
            var dbSet = await GetDbSetAsync();
            var query = dbSet.AsQueryable();

            // 基本过滤
            if (!string.IsNullOrWhiteSpace(filter))
            {
                query = query.Where(g =>
                    g.FullName.Contains(filter) ||
                    g.MainContact.Contains(filter) ||
                    g.Address.Contains(filter) ||
                    g.Phone.Contains(filter) ||
                    g.Mobile.Contains(filter));
            }

            return await query.CountAsync();
        }

        public async Task<List<Gongying>> SearchByNameAsync(
            int skipCount,
            int maxResultCount,
            string sorting,
            string nameKeyword)
        {
            var dbSet = await GetDbSetAsync();
            var query = dbSet.AsQueryable();

            // 按名称搜索
            if (!string.IsNullOrWhiteSpace(nameKeyword))
            {
                query = query.Where(g => g.FullName.Contains(nameKeyword));
            }

            // 排序
            if (!string.IsNullOrWhiteSpace(sorting))
            {
                query = query.OrderBy(sorting);
            }
            else
            {
                query = query.OrderBy(g => g.Id);
            }

            // 分页
            return await query
                .Skip(skipCount)
                .Take(maxResultCount)
                .ToListAsync();
        }

        public async Task<int> GetCountByNameAsync(string nameKeyword)
        {
            var dbSet = await GetDbSetAsync();
            var query = dbSet.AsQueryable();

            // 按名称搜索
            if (!string.IsNullOrWhiteSpace(nameKeyword))
            {
                query = query.Where(g => g.FullName.Contains(nameKeyword));
            }

            return await query.CountAsync();
        }
    }
} 